package com.yh.workbill.login.mvp;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.yh.workbill.App;
import com.yh.workbill.AppGlobals;
import com.yh.workbill.db.UserInfo;
import com.yh.workbill.db.UserInfoDao;
import com.yh.workbill.login.LoginViewModel;
import com.yh.workbill.model.LoginSuccess;
import com.yh.workbill.network.BaseCallback;
import com.yh.workbill.network.OkHttpService;
import com.yh.workbill.utils.Config;

import androidx.arch.core.executor.ArchTaskExecutor;
import androidx.lifecycle.LiveData;

import static com.yh.workbill.utils.Config.SP_USER;

public class LoginModel {

    public interface OnLoginCallback {
        void successLogin();

        void successCheckToken();

        void fail(String message);
    }

    public void login(UserInfoDao dao, String phone, String pwd, OnLoginCallback callback) {
        OkHttpService.getInstance().login(phone, pwd, new BaseCallback<LoginSuccess>() {

            @Override
            public void accessSuccess(int code, String msg, LoginSuccess data) {
                if (code == 200) {
                    //由于数据库没有开启在主线程的策略 所以这里添加数据到数据库用线程来处理
                    ArchTaskExecutor.getIOThreadExecutor().execute(new Runnable() {
                        @Override
                        public void run() {
                            UserInfo check_userinfo = dao.getSingleUserInfo(phone);
                            UserInfo userInfo = new UserInfo(data.getUuid(), phone, data.getToken());
                            if (check_userinfo == null) {
                                dao.add(userInfo);
                            } else {
                                dao.update(phone, data.getToken());
                            }
                            SharedPreferences.Editor sp = AppGlobals.getApplication().getSharedPreferences(SP_USER, AppGlobals.getApplication().MODE_PRIVATE).edit();
                            sp.putString("phone", phone);
                            sp.apply();
                        }
                    });
                    callback.successLogin();
                } else {
                    callback.fail(msg);
                }
            }

            @Override
            public void accessFailure(String errorMessage) {
                callback.fail(errorMessage);
            }
        });
    }

    public void chechToken(String token, OnLoginCallback callback) {
        SharedPreferences sp = AppGlobals.getApplication().getSharedPreferences(SP_USER, AppGlobals.getApplication().MODE_PRIVATE);
        String phone = sp.getString("phone", "");
        if (phone != null && !phone.equals("")) {
            OkHttpService.getInstance().checkToken("Bearer " + token, new BaseCallback<String>() {
                @Override
                public void accessSuccess(int code, String msg, String data) {
                    if (code == 200) {
                        callback.successCheckToken();
                    } else {
                        callback.fail(msg);
                    }
                }

                @Override
                public void accessFailure(String errorMessage) {
                    callback.fail(errorMessage);
                }
            });
        } else {
            callback.fail("");
        }
    }


}
